package com.edu.zut.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.zut.entity.RoleMenu;
import com.edu.zut.mapper.RoleMenuMapper;
import com.edu.zut.result.HttpCode;
import com.edu.zut.result.ResponseResult;
import com.edu.zut.service.RoleMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 许文科
 * @since 2023-01-12
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    public ResponseResult edit(RoleMenu roleMenu) {
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleMenu.getRoleId());
        wrapper.eq("menu_id", roleMenu.getMenuId());
        RoleMenu roleMenu1 = roleMenuMapper.selectOne(wrapper);
        //如果不为空，则表中已有数据，执行删除操作
        if (roleMenu1 != null) {
            QueryWrapper<RoleMenu> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("role_id", roleMenu.getRoleId());
            wrapper1.eq("menu_id", roleMenu.getMenuId());
            roleMenuMapper.delete(wrapper1);
            return new ResponseResult(HttpCode.SUCCESS, "权力已收回.", null);
        }
        //如果为空，执行添加操作
        roleMenuMapper.insert(roleMenu);
        return new ResponseResult(HttpCode.SUCCESS, "已授权.", null);
    }
}
